package com.fruits.controller;

import com.base.controller.BaseController;
import com.fruits.common.Shop;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

public class ShopController extends BaseController {

	@Override
	protected Model<?> getDao() {
		return Shop.dao;
	}

	@Override
	public void list() {
		String select = "select * ";
		String sqlExceptSelect = " from " + getTable().getName() + " where  isAdmin=? order by id desc";
		Object[] sqlValues = new Object[] {  0 };
		setSqlConfig(select, sqlExceptSelect, sqlValues);
		super.list();
	}

	public void form() {
		Model<?> model = getModel(getDao().getClass(), getModelName());
		if (model.get("id") != null) {
			Model<Shop> shop = getDao().findById(model.get("id"));
			setAttr(getModelName(), shop);
			// Todo 这里还要取微信用户 Db.find("select * from ");
			findWxUser(shop.get("openid"));
		}
		renderFreeMarker("/pages/model/" + getModelName() + "/" + getModelName() + "_insert.ftl");
	}

	public void formAdmin() {
		Record shop = Db.findFirst("select * from tbl_shop where isAdmin=?", 1);
		if (shop.get("openid") != null) {
			findWxUser(shop.get("openid"));
		}
		setAttr("shop", shop);
		renderFreeMarker("/pages/model/" + getModelName() + "/" + getModelName() + "_insert.ftl");
	}

	private void findWxUser(Object openid) {
		Record wxuser = Db.findFirst("select * from sys_wx_user where openid=?", openid);
		setAttr("wxuser", wxuser);
	}
}
